package com.qf.fmall.shopcard.controller;

import com.qf.fmall.shopcard.entity.ShoppingCart;
import com.qf.fmall.shopcard.service.IShoppingCartService;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leon
 * @since 2024-01-19
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;


    @GetMapping("/listbycids")
    public ResultVo listbycids(@RequestParam("cids") List<String> cids) {

       List<ShoppingCart> data = shoppingCartService.listbycids(cids);

        return new ResultVo(10000, "success", data );
    }









    @PutMapping("/update/{cid}/{num}")
    public ResultVo update(@PathVariable("cid") Integer cid ,@PathVariable ("num")Integer num){

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCartId(cid);
        shoppingCart.setCartNum(num+"");
        shoppingCartService.updateById(shoppingCart);
        return new ResultVo(10000, "success", null);
    }







@RequestMapping("/list")
    public ResultVo list(Integer userId){

    List<ShoppingCart> data  = shoppingCartService.listCart(userId);

    return new ResultVo(10000, "success", data);


    }



    @RequestMapping("/add")
    public ResultVo add( @RequestBody ShoppingCart shoppingCart){

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String time =  simpleDateFormat.format(date);
        shoppingCart.setCartTime(time);
        shoppingCartService.save(shoppingCart);

        return new ResultVo(10000, "success", null);

    }

}
